<template>
  <div class="add-edit-dict-type-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.type_name')" prop="name">
        <el-input v-model="ruleForm.name" :placeholder="$t('tables_title.type_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.type_code')" prop="code" >
        <el-input v-model="ruleForm.code" :placeholder="$t('tables_title.type_code')" v-if="!isEdit"></el-input>
        <el-input v-model="ruleForm.code" disabled v-else></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sys_dict')" prop="systemFlag">
        <el-radio-group v-model="ruleForm.systemFlag" v-if="!isEdit">
          <el-radio label="Y">{{$t('tables_title.yes')}}</el-radio>
          <el-radio label="N">{{$t('tables_title.no')}}</el-radio>
        </el-radio-group>
        <el-radio-group v-model="ruleForm.systemFlag" disabled v-else>
          <el-radio label="Y">{{$t('tables_title.yes')}}</el-radio>
          <el-radio label="N">{{$t('tables_title.no')}}</el-radio>
        </el-radio-group>
        <div style="line-height:24px;color:#999">{{$t('dict_management.dict_warning')}}</div>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sort')" prop="sort">
        <el-input v-model="ruleForm.sort" :placeholder="$t('tables_title.sort')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.dict_desc')">
        <el-input v-model="ruleForm.description" :placeholder="$t('tables_title.dict_desc')" type="textarea"></el-input>
      </el-form-item>

      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>

import { editDictType,addDictType } from '@/api/dictApi.js'

export default {
  data(){
    return{
      loading:false,
      ruleForm:{
        name:'',
        code:'',
        systemFlag:'Y',
        sort:'',
        description:''
      },
      rules:{
          name: [
            { required: true, message: this.$t('dict_management.empty_type_name'), trigger: 'blur' },
          ],
          code: [
            { required: true, message: this.$t('dict_management.empty_type_code'), trigger: 'blur' },
          ],
          sort: [
            { required: true, message: this.$t('global.empty_sort'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ['isEdit','editDictTypeInfo'],
  watch:{
    editDictTypeInfo(newVal,oldVal){
      this.ruleForm = newVal
      this.ruleForm.systemFlag = this.ruleForm.systemFlag?this.ruleForm.systemFlag:'Y'
    }
  },
  components:{

  },
  created(){
    this.ruleForm = this.editDictTypeInfo
    this.ruleForm.systemFlag = this.ruleForm.systemFlag?this.ruleForm.systemFlag:'Y'
  },
  methods:{
    getDictTreeByTypeId(){
      getDictTree({dictTypeId:this.editDictTypeInfo.dictTypeId}).then(res=>{
        let data = getDictTreeData(res.data)
        this.treeData = res.data.filter(item=>{
          return item.id == 0
        })
        if(this.treeData.length > 0){
          this.treeData[0].children = data
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.ruleForm.dictTypeId){
            this.userEditDictType()
          }else{
            this.userAddDictType()
          }
        } else {
          return false;
        }
      });
    },
    userEditDictType(){         //修改role信息
      this.loading = true
      editDictType(this.ruleForm).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{
         this.loading = false
      })
    },
    userAddDictType(){          //新增管Role
      this.loading = true
      addDictType(this.ruleForm).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{
         this.loading = false
      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-dict-type-view{
  width: 100%;
  box-sizing: border-box;
  padding: 0 30px;
}
</style>
